import Vue from 'vue'
import { getShopPermissionRequest, getUserPermissionRequest } from '@/api/role-permission'

const rolePermission = {
  namespaced: true,
  state: {
    shopPermission: {},
    userPermission: {}
  },

  mutations: {
    SET_SHOP_PERMISSION: (state, shopPermission) => {
      state.shopPermission = shopPermission
    },
    SET_USER_PERMISSION: (state, userPermission) => {
      state.userPermission = userPermission
    }
  },

  actions: {
    getShopPermissionAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getShopPermissionRequest(parameters)
          .then(response => {
            if (response && response.result) {
              const info = response.result
              if (info) {
                commit('SET_SHOP_PERMISSION', info)
              }
              resolve(response)
            } else {
              resolve(false)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    getUserPermissionAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getUserPermissionRequest(parameters)
          .then(response => {
            if (response && response.result) {
              const info = response.result
              if (info) {
                commit('SET_USER_PERMISSION', info)
              }
              resolve(response)
            } else {
              resolve(false)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default rolePermission

